package com.zx.base.widget;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zx.tradeclient.R;

/**
 * Custom layout, used to display the sliding page.
 * 
 * @author Beanu
 * 
 * <br>
 *         To use this layout,you have two ways.
 *         <ul>
 *         <li>java code</li>
 *         <li>xml</li>
 *         </ul>
 * <br>
 *         1.java code
 * 
 *         <pre>
 * SlidingPage page=new SlidingPage(context,int,int);
 * addView(page);
 * </pre>
 * 
 * <br>
 *         2.xml
 * 
 *         <pre>
 *  android:id="@+id/sliding"
 *  android:layout_width="match_parent"
 *  android:layout_height="wrap_content"
 *  page:nav_cursor_img="@drawable/category_item"
 *  page:nav_count="6"
 * </pre>
 */
public class SlidingPage extends LinearLayout {

	private TextView[] mTextViews;
	public ViewPager mPager;
	private ImageView mCursor;

	private int offset = 0;
	private int currIndex = 0;
	private int bmpW;

	private ViewPagerSelectListener listener;

	public interface ViewPagerSelectListener {
		public void pageSelected(int current);
	};

	public SlidingPage(Context context, int count, int imgId) {
		super(context);
		init(context, count, imgId);
	}

	public SlidingPage(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray array = context.obtainStyledAttributes(attrs,
				R.styleable.slidingPage);
		int count = array.getInt(R.styleable.slidingPage_nav_count, 2);
		int imgId = array.getResourceId(R.styleable.slidingPage_nav_cursor_img,
				R.drawable.page_slider_3);
		init(context, count, imgId);

		array.recycle();
	}

	private void init(Context context, int count, int imgId) {
		this.setOrientation(VERTICAL);

		FrameLayout frameLayout = new FrameLayout(context);

		LinearLayout lineLayout = new LinearLayout(context);
		lineLayout.setBackgroundResource(R.drawable.index_nav_bg);

		if (count > 0) {
			// for titles
			mTextViews = new TextView[count];
			for (int i = 0; i < count; i++) {
				mTextViews[i] = new TextView(context);
				mTextViews[i].setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f);
				mTextViews[i].setTextColor(getResources().getColor(
						R.color.black));
				mTextViews[i].setOnClickListener(new TextViewClickListener(i));
				mTextViews[i].setGravity(Gravity.CENTER_HORIZONTAL
						| Gravity.CENTER_VERTICAL);
				LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
						LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
				p.weight = 1.0f;
				p.gravity = Gravity.CENTER;
				mTextViews[i].setLayoutParams(p);
				lineLayout.addView(mTextViews[i]);
			}

			// for cursor
			mCursor = new ImageView(context);
			mCursor.setImageResource(imgId);
			mCursor.setScaleType(ScaleType.MATRIX);

			Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imgId);
			bmpW = bitmap.getWidth();
			DisplayMetrics dm = new DisplayMetrics();
			((Activity) context).getWindowManager().getDefaultDisplay()
					.getMetrics(dm);
			int screenW = dm.widthPixels;
			offset = (screenW / count - bmpW) / 2;
			Matrix matrix = new Matrix();
			matrix.postTranslate(offset, 0);
			mCursor.setImageMatrix(matrix);

			frameLayout.addView(lineLayout, new FrameLayout.LayoutParams(
					FrameLayout.LayoutParams.MATCH_PARENT,
					FrameLayout.LayoutParams.WRAP_CONTENT));
			FrameLayout.LayoutParams param = new FrameLayout.LayoutParams(
					FrameLayout.LayoutParams.MATCH_PARENT,
					FrameLayout.LayoutParams.WRAP_CONTENT);
			param.gravity = Gravity.BOTTOM;
			frameLayout.addView(mCursor, param);
		}

		this.addView(frameLayout, new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		// for ViewPage
		mPager = new ViewPager(context);
		mPager.setOnPageChangeListener(new MyOnPageChangeListener());
		LinearLayout.LayoutParams param = new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		param.gravity = Gravity.CENTER;
		param.weight = 1.0f;
		this.addView(mPager, param);
	}

	/**
	 * set navigation titles
	 * 
	 * @param titles
	 */
	public void setTitles(String[] titles) {
		if (titles != null && mTextViews != null) {
			if (titles.length == mTextViews.length) {
				for (int i = 0; i < mTextViews.length; i++) {
					mTextViews[i].setText(titles[i]);
				}
			}
		}
	}

	public ViewPager getViewPager() {
		return mPager;
	}

	public void resetCursor() {
		if (mCursor != null) {
			int one = offset * 2 + bmpW;
			Animation animation = new TranslateAnimation(one * currIndex, one* currIndex, 0, 0);
			animation.setFillAfter(true);
			animation.setDuration(0);
			mCursor.startAnimation(animation);
		}
	}

	public void setViewPageSelectedListener(ViewPagerSelectListener listener) {
		this.listener = listener;
	}

	public ViewPagerSelectListener getViewPageSelectedListener() {
		return listener;
	}

	private class TextViewClickListener implements View.OnClickListener {
		private int index = 0;

		public TextViewClickListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			mPager.setCurrentItem(index);
		}
	};

	/**
	 * 
	 * @author Beanu
	 * 
	 */
	private class MyOnPageChangeListener implements OnPageChangeListener {

		int one = offset * 2 + bmpW;

		@Override
		public void onPageSelected(int arg0) {
			Animation animation = new TranslateAnimation(one * currIndex, one
					* arg0, 0, 0);

			mTextViews[currIndex].setTextColor(getResources().getColor(
					R.color.black));
			currIndex = arg0;
			animation.setFillAfter(true);
			animation.setDuration(300);
			animation.setAnimationListener(new AnimationListener() {

				@Override
				public void onAnimationStart(Animation animation) {

				}

				@Override
				public void onAnimationRepeat(Animation animation) {

				}

				@Override
				public void onAnimationEnd(Animation animation) {
					if (listener != null) {
						listener.pageSelected(currIndex);
					}
				}
			});
			mCursor.startAnimation(animation);
			mTextViews[currIndex].setTextColor(getResources().getColor(
					R.color.nav_text_selected));

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}

	public static class SlidingPagerAdapter extends PagerAdapter {
		public List<View> mListViews;

		public SlidingPagerAdapter(List<View> mListViews) {
			this.mListViews = mListViews;
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(mListViews.get(arg1));
		}

		@Override
		public void finishUpdate(View arg0) {
		}

		@Override
		public int getCount() {
			return mListViews.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(mListViews.get(arg1), 0);
			return mListViews.get(arg1);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == (arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}
	}
}
